package com.wzh.user.controller;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.wzh.user.util.OssUtils;
import com.wzh.user.util.ImageCaptchaUtil;
import com.wzh.user.util.Md5Util;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.user.entity.HouseUserEntity;
import com.wzh.user.service.HouseUserService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 用户
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-22 19:02:40
 */
@RestController
@RequestMapping("user/houseuser")
public class HouseUserController {
    @Autowired
    private HouseUserService houseUserService;

    @Autowired
    private OssUtils ossUtils;

    @Value("${register.salt}")
    private String salt;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 获取手机验证码
     */
    @RequestMapping("/getTelCode")
    public R getTelCode(@RequestParam String tel){
        rabbitTemplate.convertAndSend("msg2",tel);
        return R.ok("发送验证码成功3分钟内有效");
    }

    /**
     * 获取上传图片
     */
    @RequestMapping("/upload")
    public R upload(@RequestParam("file")MultipartFile file){
        return ossUtils.uploadFile(file);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @SentinelResource(value = "userList",blockHandler = "handleException",blockHandlerClass = ExceptionConfig.class)
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = houseUserService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestParam String username,@RequestParam String password){
        return houseUserService.login(username,password);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId){
		HouseUserEntity houseUser = houseUserService.getById(userId);

        return R.ok().put("houseUser", houseUser).setData(houseUser);
    }

    /**
     * 注册
     */
    @RequestMapping("/register")
    public R register(@RequestBody HouseUserEntity houseUser){
        //验证码验证
        String code = redisTemplate.opsForValue().get(houseUser.getTel());
        if (code==null||!code.equals(houseUser.getCode())){
            return R.error("手机验证码有误");
        }

        //唯一判断
        Integer i=houseUserService.checkUserName(houseUser.getUsername());
        if (i>0){
            return R.error("用户名已存在");
        }
        houseUser.setPassword(Md5Util.md5salt(Md5Util.md5salt(houseUser.getPassword(),salt),salt));
        houseUser.setCreateTime(new Date());
        houseUser.setMoney(new BigDecimal(0.00));
		houseUserService.save(houseUser);
        return R.ok();

    }
    /**
     * 添加
     */
    @RequestMapping("save")
    public R save(@RequestBody HouseUserEntity userEntity){
        userEntity.setCreateTime(new Date());
        userEntity.setPassword(Md5Util.md5salt(Md5Util.md5salt(userEntity.getPassword(),salt),salt));
        boolean save = houseUserService.save(userEntity);

        return R.ok(save+"");
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HouseUserEntity houseUser){
		houseUserService.updateById(houseUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds){
		houseUserService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
